class Solution(object):
    def similarRGB(self, color):
        """
        :type color: str
        :rtype: str
        """
        def find(s):
            c1 = ord(s[0]) - ord('0')
            if 0 <= c1 <= 9:
                num1 = c1
            else:
                num1 = ord(s[0]) - ord('a') + 10
            c2 = ord(s[1]) - ord('0')
            if 0 <= c2 <= 9:
                num2 = c2
            else:
                num2 = ord(s[1]) - ord('a') + 10
            min_i = -1
            min_value = 256
            num = num1 * 16 + num2
            for i in range(num1 - 1, num1 + 2):
                if 0 <= i <= 15:
                    value = abs(i * 16 + i - num)
                    if value < min_value:
                        min_i = i
                        min_value = value
            if min_i < 10:
                return str(min_i) + str(min_i)
            else:
                return str(chr(ord('a') + min_i - 10)) * 2
        return '#' + find(color[1:3]) + find(color[3: 5]) + find(color[5:])




data = Solution()
color = "#09f166"
print(data.similarRGB(color))
color = "#4e3fe1"
print(data.similarRGB(color))
